package com.ruoyi.waterPrice.service.impl;

import java.math.BigDecimal;
import java.util.*;

import com.ruoyi.common.core.domain.base.datam.WiOrganB;
import com.ruoyi.common.core.domain.business.waterPrice.*;
import com.ruoyi.common.core.enums.FeeKindEnum;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.business.BusinessUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.waterPrice.mapper.*;
import com.ruoyi.waterPrice.service.IWiOrgacctYService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 机构水费台账登记Service业务层处理
 *
 * @author ps
 * @date 2023-02-17
 */
@Service
public class WiOrgacctYServiceImpl implements IWiOrgacctYService {

    private static final Logger log = LoggerFactory.getLogger(WiOrgacctYServiceImpl.class);

    @Autowired
    private WiOrgacctYMapper wiOrgacctYMapper;
    @Autowired
    private WiOrgfeeYMapper wiOrgfeeYMapper;
    @Autowired
    private WiOrgbookRMapper wiOrgbookRMapper;
    @Autowired
    private WiFeemodeYMapper wiFeemodeYMapper;
    @Autowired
    private WiFarmacctYMapper wiFarmacctYMapper;

    /**
     * 查询机构水费台账登记
     *
     * @return 机构水费台账登记
     */
    @Override
    public WiOrgacctY selectWiOrgacctYByFeeYrAndOrgCd(String feeYr, String orgCd) {
        WiOrgacctY wiOrgacctY = wiOrgacctYMapper.selectWiOrgacctYByFeeYrAndOrgCd(feeYr, orgCd);
        if (wiOrgacctY == null) {//说明没有记录需要去机构水费核定表把记录拉进来后返回去
            WiOrgfeeY wiOrgfeeY = wiOrgfeeYMapper.selectWiOrgfeeYByFeeYrAndOrgCd(feeYr, orgCd);
            if (wiOrgfeeY == null) {
                return null;
            }
            //向流水账添加记录
            WiOrgacctY wiOrgacctY1 = new WiOrgacctY();
            wiOrgacctY1.setFeeYr(feeYr);
            wiOrgacctY1.setOrgCd(orgCd);
            wiOrgacctY1.setTotalFee(wiOrgfeeY.getWaterFee());
            wiOrgacctY1.setCashNum(0L);
            wiOrgacctY1.setPayFee(new BigDecimal("0"));
            wiOrgacctY1.setSaveFee(wiOrgfeeY.getWaterFee());
            wiOrgacctYMapper.insertWiOrgacctY(wiOrgacctY1);
        }
        wiOrgacctY = wiOrgacctYMapper.selectWiOrgacctYByFeeYrAndOrgCd(feeYr, orgCd);
        String orgNm = wiOrgacctYMapper.selectOrgNm(orgCd);
        if (StringUtils.isNotBlank(orgNm)) {
            wiOrgacctY.setOrdNm(orgNm);
        }
        return wiOrgacctY;
    }

    /**
     * 查询机构水费台账登记列表
     *
     * @param wiOrgacctY 机构水费台账登记
     * @return 机构水费台账登记
     */
    @Override
    public List<WiOrgacctY> selectWiOrgacctYList(WiOrgacctY wiOrgacctY) {
        return wiOrgacctYMapper.selectWiOrgacctYList(wiOrgacctY);
    }

    /**
     * 新增机构水费台账登记
     *
     * @param wiOrgacctY 机构水费台账登记
     * @return 结果
     */
    @Override
    public int insertWiOrgacctY(WiOrgacctY wiOrgacctY) {
        return wiOrgacctYMapper.insertWiOrgacctY(wiOrgacctY);
    }

    /**
     * 修改机构水费台账登记
     *
     * @param wiOrgacctY 机构水费台账登记
     * @return 结果
     */
    @Override
    public int updateWiOrgacctY(WiOrgacctY wiOrgacctY) {
        //手动修改应缴费用时，要重新核算欠缴费用，且不能该变已缴费用
        WiOrgacctY wiOrgacctY1 = new WiOrgacctY();
        wiOrgacctY1.setOrgCd(wiOrgacctY.getOrgCd());
        wiOrgacctY1.setFeeYr(wiOrgacctY.getFeeYr());
        wiOrgacctY1.setTotalFee(wiOrgacctY.getTotalFee());
        wiOrgacctY1.setSaveFee(wiOrgacctY.getTotalFee().subtract(wiOrgacctY.getPayFee()));
        wiOrgacctY1.setIsOk(wiOrgacctY.getIsOk());
        wiOrgacctY1.setLastOper(SecurityUtils.getUserId() + "");
        wiOrgacctY1.setLastTime(new Date());
        wiOrgacctY1.setWriteTime(new Date());
        return wiOrgacctYMapper.updateWiOrgacctY(wiOrgacctY1);
    }

    /**
     * 批量删除机构水费台账登记
     *
     * @param feeYrs 需要删除的机构水费台账登记主键
     * @return 结果
     */
    @Override
    public int deleteWiOrgacctYByFeeYrs(String[] feeYrs) {
        return wiOrgacctYMapper.deleteWiOrgacctYByFeeYrs(feeYrs);
    }

    /**
     * 删除机构水费台账登记信息
     *
     * @param feeYr 机构水费台账登记主键
     * @return 结果
     */
    @Override
    public int deleteWiOrgacctYByFeeYr(String feeYr) {
        return wiOrgacctYMapper.deleteWiOrgacctYByFeeYr(feeYr);
    }

    /**
     * 机构水费收缴
     *
     * @param wiOrgacctY
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int payOrgFee(WiOrgacctY wiOrgacctY) throws Exception {
        WiOrgacctY wiOrgacctY1 = wiOrgacctYMapper.selectWiOrgacctYByFeeYrAndOrgCd(wiOrgacctY.getFeeYr(), wiOrgacctY.getOrgCd());
        if (wiOrgacctY1 == null) {
            return 0;
        } else {
            //查交了几笔
            WiOrgbookR wiOrgbookR = new WiOrgbookR();
            wiOrgbookR.setFeeYr(wiOrgacctY.getFeeYr());
            wiOrgbookR.setOrgCd(wiOrgacctY.getOrgCd());
            wiOrgbookR.setIsOk("1");//成功
            List<WiOrgbookR> wiOrgbookRS = wiOrgbookRMapper.selectWiOrgbookRList(wiOrgbookR);

            //向流水账添加记录
            wiOrgbookR.setCashNo(Long.parseLong((wiOrgbookRS.size() + 1) + ""));
            wiOrgbookR.setIsOk(wiOrgacctY.getIsOk());
            wiOrgbookR.setCurFee(wiOrgacctY.getPayFee());
            wiOrgbookR.setPayFee(wiOrgacctY.getPayFee());
            wiOrgbookR.setCashNum(wiOrgacctY.getAttrNum());
            wiOrgbookR.setOperId(SecurityUtils.getUserId() + "");
            wiOrgbookR.setPayTime(new Date());
            wiOrgbookR.setWriteOper(SecurityUtils.getUserId() + "");
            wiOrgbookR.setWriteTime(new Date());
            int i = wiOrgbookRMapper.insertWiOrgbookR(wiOrgbookR);
            if (i == 0) {
                return 0;
            }

            WiOrgacctY wiOrgacctY2 = new WiOrgacctY();
            wiOrgacctY2.setFeeYr(wiOrgacctY.getFeeYr());
            wiOrgacctY2.setOrgCd(wiOrgacctY.getOrgCd());
            wiOrgacctY2.setCashNum(Long.parseLong((wiOrgbookRS.size() + 1) + ""));
            wiOrgacctY2.setPayFee(wiOrgacctY1.getPayFee().add(wiOrgacctY.getPayFee()));
            wiOrgacctY2.setSaveFee(wiOrgacctY1.getTotalFee().subtract(wiOrgacctY1.getPayFee().add(wiOrgacctY.getPayFee())));
            wiOrgacctY2.setLastTime(new Date());
            wiOrgacctY2.setLastOper(SecurityUtils.getUserId() + "");
            wiOrgacctY2.setWriteTime(new Date());
            if (wiOrgacctYMapper.updateWiOrgacctY(wiOrgacctY2) == 0) {
                throw new Exception("缴费失败");
            }
            return 1;
        }
    }

    /**
     * 计收分析 -- 当年应缴实缴分析
     */
    @Override
    public List<WiOrgacctY> orgFeeIncomeAnalys(String year, String orgCd) {
        String orgCdQuery;
        HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(orgCd);
        orgCdQuery = addvAndOrgCodeInfo.get("resCode") + "";
        List<WiOrgacctY> orgFeeIncomeList = wiOrgacctYMapper.getOrgFeeIncomeAnalys(year, orgCdQuery);
        for (WiOrgacctY vo : orgFeeIncomeList) {
            String orgNm = wiOrgacctYMapper.selectOrgNm(vo.getOrgCd());
            if (StringUtils.isNotBlank(orgNm)) {
                vo.setOrdNm(orgNm);
            }
            vo.setCompleteRate(vo.getPayFee().divide(vo.getTotalFee(), 4, BigDecimal.ROUND_HALF_DOWN));
        }
        return orgFeeIncomeList;
    }

    /**
     * 历年应缴实缴分析
     *
     * @param orgCd
     */
    @Override
    public List<WiOrgacctY> orgFeeIncomeHistoryAnalys(String orgCd) {
        String orgCdQuery;
        HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(orgCd);
        orgCdQuery = addvAndOrgCodeInfo.get("resCode") + "";
        List<WiOrgacctY> res = wiOrgacctYMapper.getOrgFeeIncomeHistoryAnalys(orgCdQuery);
        for (WiOrgacctY wiOrgacctY : res) {
            String orgNm = wiOrgacctYMapper.selectOrgNm(wiOrgacctY.getOrgCd());
            if (StringUtils.isNotBlank(orgNm)) {
                wiOrgacctY.setOrdNm(orgNm);
            }
        }
        return res;
    }

    /**
     * 历年应缴实缴分析 柱状图
     *
     * @param orgCd
     * @return
     */
    @Override
    public List<WiOrganB> getChildOrg(String orgCd) {
        String orgCdQuery;
        HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(orgCd);
        orgCdQuery = addvAndOrgCodeInfo.get("resCode") + "";
        List<WiOrganB> WiOrganBList = wiOrgacctYMapper.getChildOrg(orgCdQuery);
        return WiOrganBList;
    }
}
